// pages/join/supplier.js
const app = getApp()
Page({

    data: {},
    onLoad: function(options) {

    },

    validate(key, value) {
        this.setData({
            startInput: true
        })
        if (key == "mobile") { //手机号校验
            let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
            this.setData({
                [`${key}`]: reg.test(value) && value ? value : '',
            })
        } else if (key == "idCard") { //身份证校验
            let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
            this.setData({
                [`${key}`]: reg.test(value) && value ? value : '',
            })
        } else {
            this.setData({
                [`${key}`]: value ? value : '',
            })
        }
    },
    bindinput(evt) {
        let key = evt.currentTarget.dataset.key
        let value = evt.detail.value
        this.validate(key, value)
    },
    submit(evt) {
        let {
            goods_name = "", supplier_range = "", city = "", name = "", mobile = "", submiting = false
        } = this.data
        let cid = wx.getStorageSync('cid')
        let obj = evt.detail.value
        Object.keys(obj).forEach((key) => {
            this.validate(key, obj[key])
        });
        if (!goods_name || !supplier_range || !city || !name || !mobile || submiting) {
            return
        }
        this.setData({
            submiting: true
        })
        let data = {
            community_id: cid ? cid : 0,
            goods_name: goods_name,
            supplier_range: supplier_range,
            city: city,
            name: name,
            mobile: mobile,
        }
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('advertisement/apply-supplier', {
            data
        }).then(res => {
            wx.hideLoading()
            wx.showModal({
                title: '温馨提示',
                content: res.data.msg,
                showCancel: false,
                confirmColor: '#ff5400',
                success: res => {
                    if (res.confirm) {
                       wx.navigateBack({
                           
                       })
                    }
                }
            })
        }).catch(err => {
            this.setData({
                submiting: false
            })
            wx.hideLoading()
        })
    },
})